<?php 
session_start();
require_once ("class/m_service.php");

	$m = new M_service();
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				$header= $m->getDataDetail($id);
				
				$result= $m->getProductList($id);
				
				$rows=$result->RecordCount();
				
				$html="";
				if ($rows>0){
					while($row = $result->FetchRow()){
						$html.="<tr>"
							."<td align='right'>".$row["ID"]."</td>"
							."<td align='left'>".$row["ProductName"]."</td>"
							."<td align='right'>".$row["Amount"]."</td>"
							."<td align='center'><img src='images/remove16.png' style='cursor:pointer' id='btnRemove' border=0/></td>"
							."</tr>";
					}
				}
				
				$ar= array("main"=>json_encode($header),
								"detail"=>$html);
				echo json_encode( $ar );
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$id=0;
			if ($ar["Type"]=='1'){
				$ar["CreatedBy"]=$_SESSION['username'];
				$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
				$id=$m->InsertData($ar);
			}else{
			
				$ar["CreatedBy"]=$_SESSION['username'];
				$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
				$id=$m->InsertData($ar);
				
				$list=$ar['Detail'];
				$sql=" Insert into tb_product_details (ProductID,SubProductID,Amount) values ";
				$sqlDetail=""; 
				 foreach ($list as $key => $project_id) {
						//print_r($list[$key]. ' ');
					$sqlDetail.=",(".$id.",".$key.",".$list[$key]." ) ";
				 }
				 //remove first comma
				$sqlDetail = substr($sqlDetail,1);
				
				$m->Excute($sql.$sqlDetail);			
				//echo $sql.$sqlDetail;				
			}
		
			echo "<tr>".genTable($m->getDataDetail($id),$_POST['rows']+1)."</tr>"; 

		
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);
				$ar["UpdatedBy"]=$_SESSION['username'];
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				if ($ar["Type"]=='1'){
					$sqlScript="select * from tb_product where 1=1 and ID='". $_POST['id'] ."'";
					$m->updateData($sqlScript, $ar);
					//delete product detail
					$m->delProductDetail($_POST['id']);
					
				}else{
					if ($ar["Type"]=='4')
						$sqlScript="select UpdatedBy,UpdatedDate, Type, ProductName, Category, MainUnit, SubUnit, Scale, Price  ,Times from tb_product where 1=1 and ID='". $_POST['id'] ."'";
					else 
						$sqlScript="select UpdatedBy,UpdatedDate, Type, ProductName, Category, MainUnit, SubUnit, Scale, Price  from tb_product where 1=1 and ID='". $_POST['id'] ."'";
						
					
					$m->updateData($sqlScript, $ar);
					
					//delete product detail
					$m->delProductDetail($_POST['id']);
					
					$list=$ar['Detail'];
					$sql=" Insert into tb_product_details (ProductID,SubProductID,Amount) values ";
					$sqlDetail=""; 
					 foreach ($list as $key => $project_id) {
						//print_r($list[$key]. ' ');
						$sqlDetail.=",(".$_POST['id'].",".$key.",".$list[$key]." ) ";
					 }
					 //remove first comma
					$sqlDetail = substr($sqlDetail,1);
				//	print $sql.$sqlDetail;
					$m->Excute($sql.$sqlDetail);			
				}
				
				//return value
				echo genTable($m->getDataDetail($_POST['id']),$_POST['rows']);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$r=$m->delData($o["id"]);

			}
		}else if ($_POST['m']== 'search'){
			$sname=$_POST['sname'];
			$ename=$_POST['ename'];
 
			$page=$_POST['page'];
			
			$page=isset($page)?$page:1;
			
			$result=$m->getData($sname,$ename,$page);
			$rows=$m->getRowCount();
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			
			if ($rows>0){
				$pID='0';
				$no=0;
				$flag=0;
				while($row = $result->FetchRow()){
					if ($pID != $row['ProductID']){
						$no++;
						$flag=1;
					}else{
						$flag=0;						
					}
					If($no%2 <> 0){
						$html .= "<tr class=row1 >";
					} else{
						$html .= "<tr>";    
					}
					
					$pID=$row['ProductID'];
					$html .= genTable($row,$no,$flag)."</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan='7' align='center' valign='middle' ><img src='./images/info.gif'/> <b/></td>"
					."</tr>";				
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html,
									'rows'=>$rows,
									'totalpage'=>$m->getTotalPage()
							);
			echo json_encode( $ar_result );
		}
	}else{ 
		if (isset($_GET['c']) && $_GET['c'] !="" ){
		 echo json_encode( $m->getDataCBO($_GET['c']));
		}
	}

	function genTable($row,$i,$f='0'){
		global $serviceScreen;
		
		
		$s.="<td align=center  >".($f==0?'':$i)."</td>
			<td style='display:none'>".$row["ID"]."</td>
			<td align='left'>". ($f==0?'':$row['ProductName'])."</td>
			<td align='center'>".$row["FirstName"].' '.$row["LastName"]."</td>
			<td align='center'>".$row["PositionName"]."</td>
			<td align='center'>".$row["Commission"] . $row["ComsType"]."</td>";
				
			
		if ($_SESSION["permission"][$serviceScreen]["Edit"]=="1") 
			if($f==0)
			$s.="<td>&nbsp;</td>";
			else
			$s.="<td align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
		else
			$s.="<td>&nbsp;</td>";
			
		if ($_SESSION["permission"][$serviceScreen]["Deleted"]=="1") 
			 if($f==0)
			$s.="<td>&nbsp;</td>";
			else
			$s.="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;
		else 
			$s.="<td>&nbsp;</td>";
			
		return $s;
	}

?>